﻿package net.wangit.view.unit.grid;

import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.view.unit.UnitComponent;
import net.wangit.view.util.Html;

public class TopNavigatorCard extends Card {

	public TopNavigatorCard(ActionContext ac, UnitComponent unit) {
		super(ac, unit);
	}

	protected void writeRow(ActionContext ac, StringBuffer html, Row row,
			int rowIndex, String rowCss) {
		beginCardTable(html);
		if (groups.size() > 0)
			writeTabs(html);
		separateGrid(html);
		super.writeRow(ac, html, row, rowIndex, rowCss);
		endCardTable(html);
	}

	protected void writeTabs(StringBuffer html) {
		html
				.append("<table id=\"tabs_")
				.append(uc.getId())
				.append(
						"\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" class=\"")
				.append("CT").append("\"><tr>");
		html.append("<td><div class=\"").append("CS0").append("\"></div><td>");
		for (int i = 0; i < groups.size(); i++) {
			Group group = (Group) groups.get(i);
			html.append("<td onmousedown=\"_showCard('").append(
					group.getGroupId()).append("')\"");
			Html.setAttribute(html, group.getGroupAttribute());
			html.append("><div id=\"").append(group.getGroupId()).append(
					"\" class=\"").append(i != 0 ? "C0" : "C1").append("\">")
					.append(group.getGroupName()).append("</div></td>");
			if (i == groups.size() - 1)
				html.append("<td class=\"").append("CSX").append(
						"\">&nbsp;<td>");
			else
				html.append("<td><div class=\"").append("CS").append(
						"\"></div><td>");
		}

		html.append("</tr></table>");
	}

	protected void separateGrid(StringBuffer html) {
		html.append("</td></tr><tr><td class=\"").append("CG").append("\">");
	}

	private void beginCardTable(StringBuffer html) {
		html
				.append("<table id=\"")
				.append("u_c_")
				.append(uc.getId())
				.append(
						"\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td>");
		html
				.append(
						"<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" class=\"")
				.append("CA").append("\"><tr><td>");
	}

	private void endCardTable(StringBuffer html) {
		html.append("</td></tr></table>");
		html.append("</td></tr></table>");
	}
}
